package com.universal_library.weight;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.universal_library.R;


/**
 * Created by solin on 2017/4/24.
 * 自定义的标题
 */

public class MyTitleBar extends FrameLayout {
    private static int EXT_PADDING_TOP;
    private Context mContext = null;
    private TextView mTitle;
    private ImageView mIcon, iv_back, iv_close;

    public MyTitleBar(@NonNull Context context) {
        this(context, null);
    }

    public MyTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyTitleBar(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs, defStyleAttr);
    }

    private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        inflater.inflate(R.layout.layout_title_bar, this, true);
        mTitle = (TextView) findViewById(R.id.tv_title_text);
        mIcon = (ImageView) findViewById(R.id.ic_icon);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_close = (ImageView) findViewById(R.id.iv_close);
        if (attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.TitleBar);
            String title = a.getString(R.styleable.TitleBar_aTitle);
            Drawable drawable = a.getDrawable(R.styleable.TitleBar_aIcon);
            a.recycle();
            mTitle.setText(title);
            mIcon.setImageDrawable(drawable);
        } else {
            mIcon.setVisibility(GONE);
        }
        setPadding(getLeft(), getTop() + getExtPaddintTop(getResources()), getRight(), getBottom());
    }

    public void setTitle(@StringRes int title) {
        if (title <= 0)
            return;
        mTitle.setText(title);
    }

    public void setTitle(String title) {
        if (TextUtils.isEmpty(title))
            return;
        mTitle.setText(title);
    }

    public void setIcon(@DrawableRes int iconRes) {
        if (iconRes <= 0) {
            mIcon.setVisibility(GONE);
            return;
        }
        mIcon.setImageResource(iconRes);
        mIcon.setVisibility(VISIBLE);
    }

    public void setBackGroundColor(@ColorRes int color) {
        if (color <= 0)
            return;
        this.setBackgroundColor(getResources().getColor(color));
    }

    public void setBackIcon(@DrawableRes int backIcon) {
        if (backIcon <= 0) {
            iv_back.setVisibility(GONE);
            return;
        }
        iv_back.setImageResource(backIcon);
        iv_back.setVisibility(VISIBLE);
    }

    /**
     * 中间显示图片的内容
     *
     * @param icon
     */
    public void setMTitleBackGroudIcon(@DrawableRes int icon) {
        if (icon <= 0)
            return;
        mTitle.setBackground(getResources().getDrawable(icon));
    }

    public void setCloseIcon(@DrawableRes int closeIcon) {
        if (closeIcon <= 0) {
            iv_close.setVisibility(GONE);
            return;
        }
        iv_close.setImageResource(closeIcon);
        iv_close.setVisibility(VISIBLE);
    }

    public void setTitleColor(@ColorRes int colorId) {
        if (colorId <= 0) {
            return;
        }
        mTitle.setTextColor(getContext().getResources().getColor(colorId));
    }

    public void setIconOnClickListener(OnClickListener listener) {
        mIcon.setOnClickListener(listener);
    }

    public void setBackClickListener(OnClickListener listener) {
        iv_back.setOnClickListener(listener);
    }

    public void setCloseListener(OnClickListener listener) {
        iv_close.setOnClickListener(listener);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        float d = getResources().getDisplayMetrics().density;
        int minh = (int) (d * 36) + getExtPaddintTop(getResources());
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(minh, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec + 60);

    }

    private int getExtPaddintTop(Resources resources) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && EXT_PADDING_TOP == 0) {
            try {
                Class<?> clazz = Class.forName("com.android.internal.R$dimen");
                Object object = clazz.newInstance();
                int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
                EXT_PADDING_TOP = resources.getDimensionPixelOffset(height);
            } catch (Exception e) {
                    e.printStackTrace();
            }
        }
        return EXT_PADDING_TOP;
    }

}
